define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teacher/index' + location.search,
                    add_url: 'teacher/add',
                    edit_url: 'teacher/edit',
                    del_url: 'teacher/del',
                    multi_url: 'teacher/multi',
                    table: 'teacher',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showExport: false,
                showToggle: false,
                showColumns: false,
                commonSearch: true,
                searchFormVisible: true,
                searchFormTemplate: 'searchformtpl',
                queryParams: function (params) {
                    $.extend(params, {
                        college_id: $("#college_id").val()
                    });
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'nickname', title: __('Name'),operate:'like'},
                        {field: 'tel', title: __('Phone'),operate:'like'},
                        {field: 'department.college.name', title: __('单位'),operate:false},
                        {field: 'department.name', title: __('部门'),operate:false},
                        {field: 'type_text', title: __('人员类型'),operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $("#department_id").change(function () {

            });
            require(['selectpage'], function () {
                 var type_data= [
                    {id:1 ,name:'在编人员'},
                    {id:2 ,name:'编外人员'},
                    {id:3 ,name:'务工人员'},
                ];
                 $('#type').selectPage({
                    showField : 'name',
                    keyField : 'id',
                    orderBy : ['id asc','name'],
                    data : type_data,
                    pagination :false,
                 })
                $('#college_id').selectPage({
                    showField: 'name',
                    keyField: 'id',
                    pagination :false,
                    data : 'college/indexsearch',
                     eAjaxSuccess : function(d){
                        var result;
                        if (d) {
                            result = d;
                        }
                        else result = undefined;
                        return result;

                    },
                    eSelect: function (data) {
                        console.log(data);
                        $.post("department/indexsearch",{
                        //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                            custom: {college_id: data.id},
                            keyField:'id',
                            showField:'name',
                            pageSize: 999,
                            },function(result){
                            console.log(result);
                            //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                            $('#department_id').selectPageData(result.list);
                        });
                        // if(data){
                        //     var arr = new Array();
                        //     $.each(city, function(i,row){
                        //         if(row.pId === data.id) arr.push(row);
                        //     });
                        //     //从城市数据中过滤省份是选择的省，并刷新城市的选择器数据
                        //     if(arr.length) $('#selectPageCity').selectPageData(arr);
                        // }
                    },
                    eClear: function(){//清空时，同时清空城市内容
                        $('#college_id').selectPageClear();
                        $('#department_id').selectPageData([]);
                    }
                })

                $('#department_id').selectPage({
                    showField: 'name',
                    keyField: 'id',
                    pagination :false,
                    data: []
                })
            })

             $('#btn-import-file').on('click', function () {
                college_id = $("#college_id").val();
                department_id = $("#department_id").val();
                if(college_id && department_id)
                {
                    require(['upload'], function (Upload) {
                        Upload.api.plupload($("#btn-import-file"), function (data, ret) {
                            console.log(data);
                            Fast.api.ajax({
                                url: 'teacher/import',
                                data: {file: data.url,college_id:college_id,department_id:department_id},
                            }, function (data, ret) {
                                table.bootstrapTable('refresh');
                            });
                        });
                    });
                }
                else
                    alert("请选择单位与科室");
                // var ids = Table.api.selectedids(table);
                // var url = options.extend.add_url;
                // if (url.indexOf("{ids}") !== -1) {
                //     url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                // }
                // Fast.api.open(url, __('Add'), $(this).data() || {});
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        ana: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teacher/ana' + location.search,
                    add_url: 'teacher/add',
                    edit_url: 'teacher/edit',
                    del_url: 'teacher/del',
                    multi_url: 'teacher/multi',
                    table: 'teacher',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showExport: false,
                showToggle: false,
                showColumns: false,
                commonSearch: true,
                searchFormVisible: true,
                searchFormTemplate: 'searchformtpl',
                queryParams: function (params) {
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    if($("#college_id").val()){
                        filter.college_id = $("#college_id").val();
                        op.college_id = "=";
                    }

                    if($("#department_id").val()){
                        filter.department_id = $("#department_id").val();
                        op.department_id = "=";
                    }

                    //if($("#is_reported").val()){
                    filter.create_time = $("#create_time").val();
                    op.create_time = "between";
                    //}

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: '', title: __('序号'),operate:false,table:table,formatter: Table.api.formatter.natureorder},
                        {field: 'nickname', title: __('Name')},
                        {field: 'tel', title: __('Phone')},
                        {field: 'college_name', title: __('单位')},
                        {field: 'department_name', title: __('科室')},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.getCollege();

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.getdepartment();
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            getdepartment: function () {
                $("#college_id").change(function () {
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "department/getbycollegeid" ,//url
                        data: 'ids='+$('#college_id').val(),
                        success: function (ret) {
                            $("#department_id").empty();
                            ret.data.forEach(function (value, index, array) {
                                $("#department_id").append("<option value='"+value.id+"'>"+value.name+"</option>");
                            });
                        },
                        error : function(error) {

                        }
                    });
                });
            },
            getCollege: function() {
                require(['selectpage'], function () {
                    // var type_data= [
                    //     {id:1 ,name:'在编人员'},
                    //     {id:2 ,name:'编外人员'},
                    //     {id:3 ,name:'务工人员'},
                    // ];
                    //  $('#type').selectPage({
                    //     showField : 'name',
                    //     keyField : 'id',
                    //     orderBy : ['id asc','name'],
                    //     data : type_data,
                    //     pagination :false,
                    //  })
                    $('#college_id').selectPage({
                        showField: 'name',
                        keyField: 'id',
                        pagination :false,
                        data : 'college/indexsearch',
                         eAjaxSuccess : function(d){
                            var result;
                            if (d) {
                                result = d;
                            }
                            else result = undefined;
                            return result;

                        },
                        eSelect: function (data) {
                            console.log(data);
                            $.post("department/indexsearch",{
                            //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                                custom: {college_id: data.id},
                                keyField:'id',
                                showField:'name',
                                pageSize: 999,
                                },function(result){
                                console.log(result);
                                //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                                $('#department_id').selectPageData(result.list);
                            });
                        },
                        eClear: function(){//清空时，同时清空城市内容
                            $('#college_id').selectPageClear();
                            $('#department_id').selectPageData([]);
                        }
                    })

                    $('#department_id').selectPage({
                        showField: 'name',
                        keyField: 'id',
                        pagination :false,
                        data: []
                    })
                })
            }
        }
    };
    return Controller;
});